﻿using System.Linq;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Windows.Data.DomainServices;
using WebHost;

namespace SilverlightShowWCFRIAColl.ViewModels
{
    public class EntityListViewModel : ViewModelBase
    {
        private BookDomainContext _context;
        /// <summary>
        /// The Context property
        /// </summary>
        public BookDomainContext Context
        {
            get
            {
                if (_context == null)
                {
                    _context = new BookDomainContext();
                }
                return _context;
            }
            set
            {
                _context = value;
                RaisePropertyChanged("Context");
            }
        }


        public RelayCommand LoadMoreBooks
        {
            get;
            private set;
        }


        public RelayCommand AddBook
        {
            get;
            private set;
        }

        public RelayCommand AddBookToEntityList
        {
            get;
            private set;
        }

        public RelayCommand DeleteBook
        {
            get;
            private set;
        }

        public RelayCommand DeleteBookFromEntityList
        {
            get;
            private set;
        }

      

        private EntityList<Book> _books;
        public EntityList<Book> Books
        {
            get
            {
                if (this._books == null)
                {
                    this._books = new EntityList<Book>(
                      this.Context.Books);
                }
                return this._books;
            }

        }


        public EntityListViewModel()
        {
            InstantiateCommands();

            // load books
            this.Books.Source = Context.Load<Book>(Context.GetBooksQuery().Take(10)).Entities;
        }


        private void InstantiateCommands()
        {
            LoadMoreBooks = new RelayCommand(() =>
            {
                this.Books.Source = Context.Load<Book>(Context.GetBooksQuery()).Entities;
            });

            AddBook = new RelayCommand(() =>
            {
                Context.Books.Add(new Book() { 
                    Author = "Kevin Dockx"
                    , ASIN = "123456"
                    , Title = "Dummy book" });
            });

            AddBookToEntityList = new RelayCommand(() =>
            {
                Books.Add(new Book() { 
                    Author = "Kevin Dockx"
                    , ASIN = "123456"
                    , Title = "Dummy book" });
            });


            DeleteBook = new RelayCommand(() =>
            {
                Context.Books.Remove(Context.Books.FirstOrDefault());
            });

            DeleteBookFromEntityList = new RelayCommand(() =>
            {
                Books.Remove(Books.FirstOrDefault());
            });
        }

    }
}
